package com.example.gyt.controller;

import com.example.gyt.pojo.Commodity;
import com.example.gyt.pojo.Messages;
import com.example.gyt.pojo.Newsl;
import com.example.gyt.pojo.PageResult;
import com.example.gyt.service.NewslService;
import com.example.gyt.service.UserService;
import com.example.gyt.util.DateUtil;
import com.example.gyt.util.ResultUtil;
import com.example.gyt.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class NewslJsonController {

    @Autowired
    private NewslService newslService;

    //添加新闻方法
    @ResponseBody
    @RequestMapping(value = "/member/addNewsl",method = RequestMethod.POST)
    public ResultUtil addNewsl(@Validated @RequestBody Newsl newsl, BindingResult result){
        try {

            //是否为空检验
            if(StringUtils.isEmpty(newsl.getPublisherId())){
                return ResultUtil.failure("发布人id不能为空");
            }
            if(StringUtils.isEmpty(newsl.getNewsTitle())){
                return ResultUtil.failure("新闻标题不能为空");
            }
            if(StringUtils.isEmpty(newsl.getNewsContent())){
                return ResultUtil.failure("新闻内容不能为空");
            }

            newsl.setId(UuidUtil.getUUID());

            //获取到当前时间
            newsl.setCreateTime(DateUtil.getCurrentTime());

            int count = newslService.insertSelective(newsl);
            if (count == 0){
                return ResultUtil.success("添加新闻失败");
            }

            return ResultUtil.success("添加新闻信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }

    }

    //删除一条新闻
    @ResponseBody
    @RequestMapping(value = "/member/deleteNewsl",method = RequestMethod.POST)
    public ResultUtil deleteNewsl(@Validated @RequestBody Newsl newsl, BindingResult result){
        try {

            if(StringUtils.isEmpty(newsl.getId())){
                return ResultUtil.failure("新闻ID不能为空");
            }

            int count = newslService.deleteByPrimaryKey(newsl.getId());
            if (count == 0){
                return ResultUtil.failure("删除新闻失败");
            }

            return ResultUtil.success("删除新闻成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }


    //新闻ID查信息
    @ResponseBody
    @RequestMapping(value = "/member/queryNewsl",method = RequestMethod.POST)
    public ResultUtil queryNewsl(@Validated @RequestBody Newsl newsl, BindingResult result){
        try {

            if(StringUtils.isEmpty(newsl.getId())){
                return ResultUtil.failure("新闻ID不能为空");
            }

            Newsl newsl1 = newslService.selectByPrimaryKey(newsl.getId());
            if (newsl1 == null){
                return ResultUtil.failure("获取新闻信息失败");
            }

            return ResultUtil.success(newsl1);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //修改新闻信息
    @ResponseBody
    @RequestMapping(value = "/member/updateNewsl",method = RequestMethod.POST)
    public ResultUtil updateNewsl(@Validated @RequestBody Newsl newsl, BindingResult result){
        try {
            if(StringUtils.isEmpty(newsl.getId())){
                return ResultUtil.failure("新闻ID不能为空");
            }

            /*if(StringUtils.isEmpty(user.getUserName())){
                user.setUserName(null);
            }

            if(StringUtils.isEmpty(user.getUserBrithday())){
                user.setUserBrithday(null);
            }

            if(StringUtils.isEmpty(user.getUserAddress())){
                user.setUserAddress(null);
            }

            if(StringUtils.isEmpty(user.getUserTel())){
                user.setUserTel(null);
            }*/

            //更新修改时间
            newsl.setUpdateTime(DateUtil.getCurrentTime());

            int count = newslService.updateByPrimaryKeySelective(newsl);
            if (count == 0){
                return ResultUtil.failure("修改新闻信息失败");
            }

            return ResultUtil.success("修改新闻信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //集合于新闻分页查询，所有新闻查询，新闻类型查询，新闻名字模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allNewslList",method = RequestMethod.POST)
    public ResultUtil allNewslList(@Validated @RequestBody Newsl newsl, BindingResult result){
        try {

            List<Newsl> newsl1 = newslService.getAllNewslList(newsl);
            if (newsl1.size() == 0){
                return ResultUtil.failure("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = newslService.queryCountLike(newsl);

            //加载实体将数据返回给用户
            PageResult pageResult = new PageResult();
            if (newsl.getPageResult() != null){
                pageResult.setCurrentPage(newsl.getPageResult().getCurrentPage());
                pageResult.setPageSize(newsl.getPageResult().getPageSize());

            }

            pageResult.setTotalSize(countLike);
            pageResult.setItems(newsl1);

            return ResultUtil.success(pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }
}
